package com.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dao.CategoryBrandRelationDao;
import com.mall.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;

import com.mall.dao.CategoryDao;
import com.mall.entity.Category;
import com.mall.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public List<Category> listWithTree() {
        //查出所有分类
        List<Category> list = categoryDao.selectList(null);
        //组装父子树结构
        List<Category> collect = list.stream().filter(category ->
                category.getParentCid() == 0
        ).map((menu)->{
            menu.setChildren(getChildren(menu,list));
            return menu;
        }).sorted((menu1, menu2) -> {
            //菜单排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());


        return collect;
    }

    @Override
    public IPage<Category> pageList(Integer current, Integer pageSize) {
        Page<Category> page = new Page<>(current, pageSize);
        IPage<Category> categoryPage = categoryDao.selectPage(page, null);
        long pages = categoryPage.getPages();
        long current1 = categoryPage.getCurrent();
        long total = categoryPage.getTotal();
        long size = categoryPage.getSize();
//        System.out.println("page" + pages);
//        System.out.println("curr"+current1);
//        System.out.println("total"+total);
//        System.out.println("size"+size);

//        List<Category> records = categoryPage.getRecords();
        return categoryPage;
    }


    /**
     * 删除批量
     * @param asList
     */
    @Override
    public void removeMenusByIds(Integer id) {
        //TODO 检查当前删除菜单是否被其他地方引用
        categoryDao.deleteById(id);
    }

    @Override
    public void updateDetail(Category category) {
        //更新
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());

    }

    //225,25,2
    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        //1、收集当前节点id
        paths.add(catelogId);
        Category byId = this.getById(catelogId);
        if(byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;

    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }




    /**
     * 递归查找菜单
     * @param category
     * @param categoryList
     * @return
     */
    private List<Category> getChildren(Category category,List<Category>categoryList){
        List<Category> child = categoryList.stream().filter(cate -> {
            return category.getCatId() == cate.getParentCid();
        }).map(cate -> {
            //找到子菜单
            cate.setChildren(getChildren(cate, categoryList));
            return cate;
        }).sorted((menu1, menu2) -> {
            //菜单排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return child;
    }


//    @Override
//    public PageUtils queryPage(Map<String, Object> params) {
//
//        IPage<Category> page = this.page(
//                new Query<Category>().getPage(params),
//                new QueryWrapper<Category>()
//        );
//
//        return new PageUtils(page);
//    }

}






















